﻿using SkiaSharp;

namespace Celestial36Tasks
{
    static class Program
    {
        static void Main()
        {
            // 创建一个位图和画布
            int width = 800;
            int height = 600;
            using var surface = SKSurface.Create(new SKImageInfo(width, height));
            var canvas = surface.Canvas;
            // 清空画布为白色
            canvas.Clear(SKColors.White);
            // 创建画笔 - 用于绘制矩形边框
            using var strokePaint = new SKPaint
            {
                Color = SKColors.Blue,
                Style = SKPaintStyle.Stroke,
                StrokeWidth = 5,
                IsAntialias = true
            };
            // 创建画笔 - 用于填充矩形
            using var fillPaint = new SKPaint
            {
                Color = SKColors.LightBlue.WithAlpha(150), // 半透明填充
                Style = SKPaintStyle.Fill,
                IsAntialias = true
            };
            // 绘制矩形
            float x = 100;
            float y = 100;
            float rectWidth = 600;
            float rectHeight = 400;
            // 先填充再描边
            canvas.DrawRect(x, y, rectWidth, rectHeight, fillPaint);
            canvas.DrawRect(x, y, rectWidth, rectHeight, strokePaint);
            // 添加文本标签
            using var textPaint = new SKPaint
            {
                Color = SKColors.Black,
                TextSize = 24,
                IsAntialias = true,
                TextAlign = SKTextAlign.Center,
                Typeface = SKTypeface.FromFamilyName(
                    "SimHei",
                    SKFontStyleWeight.Normal,
                    SKFontStyleWidth.Normal,
                    SKFontStyleSlant.Upright)
            };
            canvas.DrawText("绘制的矩形", width / 2, y - 30, textPaint);
            // 显示矩形尺寸信息
            string dimensions = $"尺寸: {rectWidth} x {rectHeight} 像素";
            canvas.DrawText(dimensions, width / 2, y + rectHeight + 60, textPaint);
            // 保存结果为PNG文件
            using var image = surface.Snapshot();
            using var data = image.Encode(SKEncodedImageFormat.Png, 100);
            using var stream = System.IO.File.OpenWrite("rectangle.png");
            data.SaveTo(stream);
            Console.WriteLine("矩形已绘制并保存为rectangle.png");
            Console.WriteLine("按任意键退出...");
            Console.ReadKey();
            // 无语！
        }
    }
}